//
//  ContentView.swift
//  Shared
//
//  Created by lee on 2021/11/12.
//

import SwiftUI

struct ContentView: View {
    
    @State var addr: String = ""
    @State var jsonString: String = ""
    
    var body: some View {
        VStack(alignment: .center, spacing: 10, content: {
            Spacer()
            
            HStack(alignment: .center, spacing: 20) {
                Text("粘贴CSV数据")
                    .font(.subheadline)
                    .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 0))
                Spacer()
            }
                
            TextEditor(text: $addr)
                .padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
            
            Button {
                self.jsonString = self.inputJsonFile()
            } label: {
                Text("生成json")
                    .foregroundColor(Color.white)
                    .padding(EdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20))
            }
            .background(Color.red)
            
            TextEditor(text: $jsonString)
                .padding(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
            
            Spacer()
        })
        .background(Color(red: 200/255.0, green: 200/255.0, blue: 200/255.0))
    }
    
    
    func inputJsonFile() -> String {
        let array = addr.components(separatedBy: "\n")
        if array.count == 0 {
            return ""
        }
        var content = [String:Any]()
        for (i, line) in array.enumerated() {
            if i > 0 {
                let lineArr = line.components(separatedBy: ",")
                if lineArr.count > 6 {
                    let province = lineArr[0]
                    let gasoline92 = lineArr[2]
                    let gasoline95 = lineArr[3]
                    let gasoline98 = lineArr[4]
                    let dieselOil0 = lineArr[5]
                    let updateDate = lineArr[6]
                    let oneProvince = [
                        "gasoline92": gasoline92,
                        "gasoline95": gasoline95,
                        "gasoline98": gasoline98,
                        "dieselOil0": dieselOil0,
                        "updateDate": updateDate,
                        "province": province
                    ]
                    print("oneProvince = \(oneProvince)")
                    content.updateValue(oneProvince, forKey: province)
                }
            }
        }
        let jsonMap = [
            "msg":"success",
            "retCode": "200",
            "result": content
        ] as [String : Any]
        
        var jsonStr = ""
        if(JSONSerialization.isValidJSONObject(jsonMap)) {
            let data: Data! = try? JSONSerialization.data(withJSONObject: jsonMap, options: .prettyPrinted)
            jsonStr = String(data: data!, encoding: .utf8) ?? ""
        }
        print("jsonStr =  \(jsonStr)")
        return jsonStr
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
